package com.example.chongwushangcheng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chongwushangcheng.model.Bidder;
import com.example.petshopping.R;
import com.example.petshopping.model.AppPetAuctionBidRespVO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.ArrayList;
import java.util.Locale;

public class BidderAdapter extends RecyclerView.Adapter<BidderAdapter.BidderViewHolder> {

    private Context context;
    private List<Bidder> bidderList;

    public BidderAdapter(Context context, List<Bidder> bidderList) {
        this.context = context;
        this.bidderList = bidderList;
    }

    @NonNull
    @Override
    public BidderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bidder, parent, false);
        return new BidderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BidderViewHolder holder, int position) {
        Bidder bidder = bidderList.get(position);
        
        // 设置出价者信息
        String displayName = bidder.isAnonymous() ? "匿名出价" : bidder.getName();
        holder.tvBidderName.setText(displayName);
        holder.tvVipLevel.setText(bidder.getVipLevel());
        holder.tvBidTime.setText(bidder.getBidTime());
        holder.tvBidAmount.setText(String.format(Locale.getDefault(), "¥%.0f", bidder.getBidAmount()));
        
        // 设置出价状态
        if (bidder.isLeading()) {
            holder.tvBidStatus.setText("领先");
            holder.tvBidStatus.setSelected(true);
            holder.tvBidStatus.setTextColor(context.getResources().getColor(R.color.highlight_red));
        } else {
            holder.tvBidStatus.setText("落后");
            holder.tvBidStatus.setSelected(false);
            holder.tvBidStatus.setTextColor(context.getResources().getColor(R.color.text_secondary));
        }
        
        // 设置头像（目前使用默认头像）
        holder.ivBidderAvatar.setImageResource(R.drawable.ic_user_avatar);
    }

    @Override
    public int getItemCount() {
        return bidderList != null ? bidderList.size() : 0;
    }

    public void updateBidders(List<Bidder> newBidders) {
        this.bidderList = newBidders;
        notifyDataSetChanged();
    }

    /**
     * 更新出价记录数据（从服务端数据转换）
     */
    public void updateBiddersFromServer(List<AppPetAuctionBidRespVO> serverBidders) {
        if (serverBidders == null) {
            this.bidderList.clear();
            notifyDataSetChanged();
            return;
        }

        List<Bidder> convertedBidders = new ArrayList<>();
        for (int i = 0; i < serverBidders.size(); i++) {
            AppPetAuctionBidRespVO serverBidder = serverBidders.get(i);
            Bidder bidder = convertFromServerData(serverBidder, i == 0); // 第一条记录为领先
            convertedBidders.add(bidder);
        }

        this.bidderList = convertedBidders;
        notifyDataSetChanged();
    }

    /**
     * 将服务端数据转换为本地Bidder对象
     */
    private Bidder convertFromServerData(AppPetAuctionBidRespVO serverBidder, boolean isLeading) {
        String displayName = serverBidder.getBidderName();
        if (displayName == null || displayName.trim().isEmpty()) {
            displayName = "匿名用户";
        }

        String vipLevel = "VIP.1"; // 默认VIP等级，实际应该从用户信息中获取
        //先格式化时间
        String bidTime = formatBidTime(serverBidder.getBidTime());

        double bidAmount = serverBidder.getBidPrice() != null ? serverBidder.getBidPrice() : 0.0;

        return new Bidder(
            displayName,
            serverBidder.getBidderAvatar(),
            vipLevel,
            bidTime,
            bidAmount,
            isLeading,
            false // 默认非匿名
        );
    }

    /**
     * 格式化出价时间
     */
    private String formatBidTime(java.time.LocalDateTime bidTime) {
        if (bidTime == null) {
            return "未知时间";
        }

        try {
            java.time.format.DateTimeFormatter formatter =
                java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return bidTime.plusHours(8).format(formatter);
        } catch (Exception e) {
            return bidTime.toString();
        }
    }

    static class BidderViewHolder extends RecyclerView.ViewHolder {
        ImageView ivBidderAvatar;
        TextView tvBidderName;
        TextView tvVipLevel;
        TextView tvBidTime;
        TextView tvBidStatus;
        TextView tvBidAmount;

        public BidderViewHolder(@NonNull View itemView) {
            super(itemView);
            ivBidderAvatar = itemView.findViewById(R.id.ivBidderAvatar);
            tvBidderName = itemView.findViewById(R.id.tvBidderName);
            tvVipLevel = itemView.findViewById(R.id.tvVipLevel);
            tvBidTime = itemView.findViewById(R.id.tvBidTime);
            tvBidStatus = itemView.findViewById(R.id.tvBidStatus);
            tvBidAmount = itemView.findViewById(R.id.tvBidAmount);
        }
    }
} 